<?php

class ToolbaseActions extends sfActions {
        
        public function getDB( $lang = 'en', $wiki = 'wikipedia' ) {
                $user = sfContext::getInstance()->getUser();
                
                if( $user->hasFlash( 'error' ) ) $user->setFlash( 'error', null );
                
                try {
                        
                        $method = 'getServerFromLang';
                        if( $lang instanceof sfWebRequest ) $method = 'getServerFromRequest';
                         
                        $db = ToolbaseDatabaseManager::getInstance()->$method( $lang, $wiki );
                }
                catch( ToolDBNoWikiException $e ) {
                        sfProjectConfiguration::getActive()->loadHelpers(array('AddonFuncs', 'I18N2'));
                        
                        $user->setFlash( 'error', i18n( 'toolbase-error-nowiki', $e->lang, $e->wiki ) );
                        return;                 
                }
                return $db;
        }
        
        public function preExecute() {
                $user = $this->getUser();
                
                $dirs = Toolbase::getPossibleLanguages();
                        
                $lang = $this->getRequest()->getParameter( 'lang', 'en' );
                $lang = $this->getRequest()->getParameter( 'uselang', $lang );
                
                if( !$lang ) {
                        $lang = $this->getRequest()->getPreferredCulture( $dirs );
                }
                
                if( !$lang || !in_array( $lang, $dirs ) ) $lang = 'en';
                
                $user->setCulture( $lang );
                
        }
        
        public function postExecute() {
                
                if( ToolbaseDatabaseManager::hasInstance() ) {
                        
                        $server = ToolbaseDatabaseManager::getInstance()->getLangServer();
                        if( !$server ) return;
                        
                        $server = $server->getReplagServer();
                        
                        
                        
                        $res = $server->getObject()->select()
                                ->from( array( 'r' => 'recentchanges' ), array( 'r.rc_timestamp', 'UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) AS replag' ) )
                                ->order( 'r.rc_timestamp DESC' )
                                ->limit( 1 )
                                ->query()
                                ->fetch();
                        
                        $seconds = floor( $res['replag'] );
                        
                        if( $seconds > 120 ) {  
                        
                                sfProjectConfiguration::getActive()->loadHelpers(array('AddonFuncs', 'I18N2'));
                        
                                $times = parse_seconds( $seconds );
                                
                                $strs = null;
                                foreach( $times as $name => $num ) {
                                        $strs[] = sprintf( '%s %s', $num, i18n( 'toolbase-replag-' . $name . 's' ) );
                                }
                                
                                $this->replag = implode( ', ', $strs );
                        }

                
                }
                
        }
        
}
